package pc.system;

import java.util.*;
import pc.util.*;
import pc.sql.*;
import pc.sql.value.*;

public class Auth extends Tasks
{
  private Record ref = new Record();
  private int count = 0;
  private String sid = null;
  //
  public java.util.Date act_at = new java.util.Date();
  private boolean debug = false;
  //
  private String user_name = null;
  private String password  = null;
  private boolean authenticated = false;

  public Auth(ConnectionPool connPool, String user_name, String password, String language)
  {
    super(connPool);
    this.setLanguage(language);
    this.user_name  = user_name;
    this.password   = password;
    //this.language   = language == null ? "us" : language;
    authenticated = this.authenticate();
    if (connPool != null)  sid = connPool.getDB_Name() + ":" + user_name;
  }

  public Auth(ConnectionPool connPool, String user_name, String password)
  {
    this(connPool,user_name,password,null);
  }

  public Auth(ConnectionPool connPool)
  {
    this(connPool,null,null);
  }

  public String get_SID()
  {
    return sid;
  }

  public boolean changePwd(String sql,String old_password, String new_password)
  {
    if (!old_password.equals(this.password)) {
      getSysLog().setMessage("Error: Incorect password");
      return false;
    }

    Vector vp = new Vector();
    vp.addElement(new StringValue(new_password));
    if (getSQLCommand().executeUpdate(sql,vp) > 0) {
      getSysLog().setMessage("Password has been changed successfully.");
      return true;
    } else {
      getSysLog().setMessage("Error: Could not change password.");
      return false;
    }
  }

  public boolean changePwd()
  {
    String old_password = getRequest().getStringValue("old_password");
    String new_password = getRequest().getStringValue("new_password");

    String sql =
        "UPDATE user_master " +
        "SET password = PASSWORD(?) " +
        "WHERE id = " + this.getUserID();
    return changePwd(sql,old_password,new_password);
  }

  public void setAuthenticate(String user_name, String password)
  {
    this.user_name = user_name;
    this.password = password;
  }

  public boolean authenticate(String sql, String user_name, String password)
  {
    if (user_name == null || password == null) return false;
    this.user_name = user_name;
    this.password = password;

    Vector vp = new Vector();
    vp.addElement(new StringValue(user_name));
    vp.addElement(new StringValue(password));
    ref.setRs(getSQLCommand().executeQuery(sql,vp));
    authenticated = ref.first();
    //this.setuid();
    return authenticated;

  }

  private boolean authenticate()
  {
	String sql ="";
	
    sql =
        "SELECT us.*,cm.name AS company_name " +
        "FROM user_master us "+
        "LEFT JOIN company_master cm ON us.company_id = cm.id "+
        "WHERE (us.user_name = ? and us.password = PASSWORD(?))";
    return authenticate(sql, user_name, password);
  }

  public void logOut()
  {
    //this.ref = new Record();
    //this.authenticated = false;
    getSQLCommand().returnConnection();
    java.util.Date now = new java.util.Date();
    String date = Utils.dateToStr(now) + " " + Utils.timeToStr(now);
    System.out.println(date + " -> " + this.getUserName() + this.getuid() + " logout. ");

    ref.close();
  }

  public void timeout()
  {
    //this.authenticated = false;
    getSQLCommand().closeConnection();
    java.util.Date now = new java.util.Date();
    String date = Utils.dateToStr(now) + " " + Utils.timeToStr(now);
    System.out.println(date + " -> " + this.getUserName() + this.getuid() + " timeout. ");
    ref.close();
  }

  public boolean Authenticated()
  {
    return this.authenticated && this.isValid();
  }

  public Record getRef()
  {
    return ref;
  }

  public String  getFullName()
  {
    return ref.getString("full_name");
  }

  public boolean  isAdmin()
  {
    return (ref.getIntValue("admin") == 1);
  }

  public String   getUserName()
  {
    return user_name;
  }

  public String   getPassword()
  {
    return password;
  }

  public String getComanyName()
  {
    return ref.getString("company_name");
  }

  public String getuid()
  {
    act_at = new java.util.Date();
    String uid = this.toString();
    int i = uid.indexOf('@');
    if (i >= 0) uid = uid.substring(i);
    return uid;
  }

/*
  public void setuid()
  {
    this.uid = this.toString();
    int i = uid.indexOf('@');
    if (i >= 0) uid = uid.substring(i);
  }
*/
  public int getUserID()
  {
    return ref.getIntValue("id");
  }

  public String getUserType()
  {
    return ref.getStringValue("type");
  }
  
  public int getCompanyID()
  {
    return ref.getIntValue("company_id");
  }

  public int getMaxLogon()
  {
    return ref.getIntValue("max_logon");
  }

/*
  public int getRight(String name)
  {
    Object obj = right_list.get(name);
    if (obj == null) obj = new Integer(0);
    return ((Integer)obj).intValue();
  }
*/
  public Tasks getTaskLevel(int level)
  {
    Tasks task = this;
    for (int i = 0; i <= level; i++) {
      task = task.findChildFocus();
      if (task == null) return null;
    }
    return task;
  }

  public boolean getDebug()
  {
    return debug;
  }

  public void setDebug(boolean debug)
  {
    this.debug = debug;
  }

  public void setCount(int count)
  {
    this.count = count;
  }

  public int getCount()
  {
    return this.count;
  }

}
